package hiatao.puzzle.fragment;

import com.desmond.parallaxheaderviewpager.TestActivity;

import haitao.puzzle.R;
import hiatao.puzzle.activity.CommentActivity;
import hiatao.puzzle.activity.LoginActivity;
import hiatao.puzzle.activity.NoticeActivity;
import hiatao.puzzle.activity.SettingActivity;
import hiatao.puzzle.activity.UserActivity;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.Tools;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MeFragment extends Fragment implements OnClickListener {

	
	private View rootView;
    private ImageView head_img;
    private TextView userName;
    private TextView unrednotice_tv;
    private View notice_ly;
    private View rl_login;
    private View comment_ly;
    private View setting_ly;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_me, container, false);
		findView();
		init();
		return rootView;
	}

	private void init() {
		rl_login.setOnClickListener(this);
		notice_ly.setOnClickListener(this);
		comment_ly.setOnClickListener(this);
		setting_ly.setOnClickListener(this);
	}

	private void findView() {
		head_img = (ImageView) rootView.findViewById(R.id.head_img);
		userName = (TextView) rootView.findViewById(R.id.userName);
		unrednotice_tv = (TextView) rootView.findViewById(R.id.unrednotice_tv);
		notice_ly =  rootView.findViewById(R.id.notice_ly);
		comment_ly =  rootView.findViewById(R.id.comment_ly);
		rl_login = rootView.findViewById(R.id.rl_login);
		setting_ly = rootView.findViewById(R.id.setting_ly);
	}
	@Override
	public void onResume() {
		super.onResume();
		if(MyApplication.getInstance().hasChage ){
			
			if (MyApplication.getInstance().user!=null) {
				ImageUtils.loadHeadImg(MyApplication.getInstance().user.getHeadUrl(), head_img);
				userName.setText(MyApplication.getInstance().user.getName());
				unrednotice_tv.setVisibility(View.VISIBLE);
			}else{
				head_img.setImageResource(R.drawable.default_head);
				userName.setText("点击登陆");
			}
			MyApplication.getInstance().hasChage = false;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.notice_ly:
			if(!MyApplication.getInstance().isLogin){
				Tools.showToast("请先登陆!");
			}else{
				getActivity().startActivity(new Intent(getActivity(),NoticeActivity.class));
				unrednotice_tv.setVisibility(View.GONE);
			}
			break;
		case R.id.comment_ly:
			if(!MyApplication.getInstance().isLogin){
				Tools.showToast("请先登陆!");
			}else{
				getActivity().startActivity(new Intent(getActivity(),CommentActivity.class));
			}
			break;
		case R.id.rl_login:
			if(!MyApplication.getInstance().isLogin){
				getActivity().startActivity(new Intent(getActivity(),LoginActivity.class));
			}else{
				getActivity().startActivity(new Intent(getActivity(),TestActivity.class));
			}
			break;
		case R.id.setting_ly:
/*			if(!MyApplication.getInstance().isLogin){
				getActivity().startActivity(new Intent(getActivity(),LoginActivity.class));
			}else{
				getActivity().startActivity(new Intent(getActivity(),UserActivity.class));
			}*/
			getActivity().startActivity(new Intent(getActivity(),SettingActivity.class));
			break;

		default:
			break;
		}
	}
}
